package com.siwei.mes.controller.erp;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.sync.erp.ErpProject;
import com.siwei.mes.service.laboratory.ContractInfoService;
import com.siwei.mes.service.laboratory.ProduceHntService;
import com.siwei.mes.service.sync.erp.ErpProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 工程信息相关  这里读取是erp传输过来的
 *
 * @author linzi
 * @since 2024-07-31 14:04:00
 */
@Api(value = "工程信息")
@RestController
@RequestMapping("erpProject")
public class ErpProjectController {
    @Resource
    private ErpProjectService erpProjectService;
    @Resource
    private ContractInfoService contractInfoService;
    @Resource
    private ProduceHntService produceHntService;

    @ApiOperation(value = "上传到协会")
    @PostMapping("/uploadLaboratory")
    public Json uploadLaboratory(@RequestBody ErpProject entity) {
        assertTrue(entity.getId() == null, "工程主键id为空");
        ErpProject erpProject = erpProjectService.loadById(entity.getId());
        if (erpProject != null) {
            /**这里操作协会的*/
            contractInfoService.saveOrUpdate(erpProject, true, true);

            /**修改对应任务单信息*/
            produceHntService.updateProduceHntByErpProject(erpProject, false);
        }
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "修改工程信息表")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) ErpProject entity) {
        /**这里工程监督号 == 报建编号*/
        entity.setBjbh(entity.getJdh());
        ErpProject erpProject = erpProjectService.update(entity);
        if (erpProject != null) {
            /**这里操作协会的*/
            contractInfoService.saveOrUpdate(erpProject, false, true);

            /**修改对应任务单信息*/
            produceHntService.updateProduceHntByErpProject(erpProject, false);

        }
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取工程信息")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<ErpProject, ErpProject> query) {
        return Json.succ().data(erpProjectService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取工程信息")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(erpProjectService.loadById(id));
    }

    @ApiOperation(value = "获取所有工程信息")
    @PostMapping("getAll")
    public Json getAll(@RequestBody ErpProject entity) {
        return Json.succ().data(ApiResult.addList(erpProjectService.getAll(entity)));
    }

    @ApiOperation(value = "工程绑定物料")
    @PostMapping("configErpProject")
    public Json configErpProject(@RequestBody ErpProject entity) {
        return Json.succ().data(ApiResult.addList(erpProjectService.configErpProject(entity)));
    }
}
